<template>
<div class="wrapper" id="container">
    <div class="content">
        <div class="city-top">
            <div class="hot-city">热门城市</div>
            <div class="city-name">
                <ul>
                  <li v-for="item in citylist.hotCities" :key="item.id"
                  @click="changecityname(item.name)">{{item.name}}</li>
              </ul>
          </div>
        </div>

         <div class="sort-top">
            <div class="sort-zimu">字母排序</div>
            <div class="sort-name">
              <ul>
                  <li v-for="(item,index) in citylist.cities" :key="index"
                  @click="changeSort(index)">{{index}}</li> 
              </ul>
          </div>
        </div>

        <div class="list" v-for="(items,index) in citylist.cities" :key="index"
        :ref="index">
            <div class="list-title" >{{index}}</div>
            <div class="list-citys" >
                <ul>
                  <li v-for="item in items" :key="item.id" @click="changecityname(item.name)">{{item.name}}</li>
              </ul>
          </div>
        </div>
    </div>
</div>
</template>

<script>
import BetterScroll from 'better-scroll'
import {mapMutations} from 'vuex'
export default {
    props:['citylist'],
    data(){
        return{
            scroll:''
        }
    },
    mounted(){
        
        this.scroll = new BetterScroll('.wrapper', {
        movable: true,
        zoom: true,
        scrollY: true,
        startY: 0,
        momentum: true,
        bounce: true
        })
    },
    methods:{
        ...mapMutations(['editcityname']
        ),
        changeSort(key){
          const h = this.$refs[key][0].offsetTop
          this.scroll.scrollTo(0,-h,2000)
        },
        changecityname(name){
            localStorage.setItem('city',name)
            this.editcityname(name)
            this.$router.push('/')
        }
    }
}
</script>

<style scoped>
.wrapper{
    position: absolute;
    left: 0;
    right: 0;
    top: 44px;
    bottom: 0;
   overflow-y:scroll;
    background-color: #f5f5f5;
}
.sort-top .sort-zimu{
    font-size: 14px; 
    height: 30px;
    line-height: 30px;
    margin-left: 10px;
}
.sort-top .sort-name ul{
    display: flex;
    width: 100%;
    flex-wrap: wrap;
    justify-content:flex-start;
    background-color: #fff;
}
.sort-top .sort-name li{
    width: 16%;
    box-sizing: border-box;
    text-align: center;
    text-decoration: none;
    font-size: 16px;
    padding: 10px 20px;
}
.city-top .hot-city{
    font-size: 14px; 
    height: 30px;
    line-height: 30px;
    margin-left: 10px;
}
.city-top .city-name ul{
    display: flex;
    width: 100%;
    flex-wrap: wrap;
    background-color: #fff;
}
.city-top .city-name li{
    border-bottom: 1px solid #ccc;
    border-right: 1px solid #ccc;
    width: 33.3%;
    box-sizing: border-box;
    text-align: center;
    text-decoration: none;
    font-size: 16px;
    padding: 10px 20px;
}
.list .list-title{
    font-size: 14px; 
    height: 30px;
    line-height: 30px;
    margin-left: 10px;
}
.list-citys{
    background-color: #fff;
}
.list-citys ul {
    width: 100%;
    display: flex;
    justify-self: start;
    flex-wrap: wrap;
}
.list-citys ul li{
    width: 25%;
    font-size: 14px;
    border-right: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    padding: 10px;
    overflow: hidden;
    box-sizing: border-box;
    text-overflow:ellipsis; 
    white-space: nowrap;
    text-align: center;
}
</style>